import { baseRequestClient } from '#/api/request'
import { requestClient } from '#/api/request'

export async function getWellsApi() {
  return baseRequestClient.get('http://localhost:5320/api/gis/xzj-well')
}

// ----------------------------------
// 查询油水井基本信息列表
// 查询油水井参数
interface WellItem {
  wellCode?: string;
  wellName?: string;
  status?: number;
}
// 查询油水井返回值
interface ListWellResult {
  rows: any;
  total: number;
}

//添加油水井参数
export interface AddWellParams extends Record<string,any> {
  wellCode: string;
  wellName: string;
  longitude: string;
  latitude: string;
  depth: number;
  status: number;
  remark: string;
  location:string;
}

//修改油水井参数
export interface EditWellParams extends Record<string,any> {
  wellCode: string;
  wellName: string;
  longitude: string;
  latitude: string;
  depth: number;
  status: number;
  remark: string;
  location:string;
}

//在gis中用于获取全部的油水井数据
export const getWellsByGis = async () => {
  const response = await requestClient.get<ListWellResult>('/manage/well/list');
  const data = response.rows; // 提取 rows 字段
  //处理数据
  const mappedResult = data.map((row: any) => {
    const location = row.location;
    let locationFormatted: [number,number] | undefined = undefined;
  
    if (location && location.startsWith('POINT')) {
      const [longitude,latitude] = location.match(/-?\d+(\.\d+)?/g)?.map(Number) || [];
      locationFormatted = [longitude,latitude];
    }
    
    return {
      ...row,
      status: row.status === 1 ? '正常' : '冻结',
      location: locationFormatted,
    };
  });
  
  //console.log("mappedResult:",mappedResult);
  return {
    items: mappedResult,
    total: response.total,
  };
}
//在油水井管理中用于获取分页数据
export const listWell = async (params: {
  pageNum: number;
  pageSize: number;
  wellCode?: string;
  wellName?: string;
  status?: string;
}) => {
  const response = await requestClient.get<ListWellResult>('/manage/well/list', { params });
  const data = response.rows; // 提取 rows 字段
  let filteredData: WellItem[] = data;
  // 过滤数据
  if (params.wellCode) {
    filteredData = filteredData.filter((item: WellItem) =>
      item.wellCode?.includes(params.wellCode ?? '') ?? false
    );
  }
  if (params.wellName) {
    filteredData = filteredData.filter((item: WellItem) =>
      item.wellName?.includes(params.wellName ?? '') ?? false
    );
  }
  if (params.status) {
    const statusNum = parseInt(params.status, 10);
    if (!isNaN(statusNum)) {
      filteredData = filteredData.filter((item: WellItem) => item.status === statusNum);
    }
  }

  const total = response.total;
  const result = filteredData;

  //处理数据
  const mappedResult = result.map((row: any) => {
    const location = row.location;
    let locationFormatted: string | undefined = undefined;
  
    if (location && location.startsWith('POINT')) {
      const [longitude, latitude] = location.match(/-?\d+(\.\d+)?/g)?.map(Number) || [];
      locationFormatted = `经度: ${longitude}, 纬度: ${latitude}`;
    }
  
    return {
      ...row,
      status: row.status === 1 ? '正常' : '冻结',
      location: locationFormatted,
    };
  });
  

  return {
    items: mappedResult,
    total: total,
  };
};

// 查询油水井基本信息详细
export async function getWell(id:any) {
  return baseRequestClient.get(`/manage/well/${id}`)
}

// 新增油水井基本信息
export async function addWell(data:AddWellParams) {
  await requestClient.post('/manage/well', data);
}

// 修改油水井基本信息
export async function updateWell(data:EditWellParams) {
  requestClient.put('/manage/well', data);
}

// 删除油水井基本信息
export async function delWell(id:number) {
  requestClient.delete(`/manage/well/${id}`)
}
